﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Linq;

namespace DataExtract
{
    public partial class frmModifyPort : Form
    {
        public Port entity;
        public frmModifyPort()
        {
            InitializeComponent();

            entity = new Port();
        }

        public frmModifyPort(Port duankou)
        {
            InitializeComponent();

            entity = duankou;
        }

        private void frmModifyPort_Load(object sender, EventArgs e)
        {
            if (entity.ID == 0)
            {
                this.Text = "添加端口";
            }
            else 
            {
                this.Text = "修改端口";

                this.txgMaxNum.Text = entity.MaxNum.ToString();
                this.txtFile.Text = entity.SendFile;
                txtPort.Text = entity.PortName.ToString();
                this.chkEnable.Checked = entity.Enabled;
            }
        }

        bool CloseCancel = false;
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtPort.Text == "")
            {
                CloseCancel = true;
                MessageBox.Show("端口不能为空");
                return;
            }

            if (txgMaxNum.Text.Trim() == "")
            {
                CloseCancel = true;
                MessageBox.Show("最大值不能为空");
                return;
            }
            try
            {
                Convert.ToInt32(txtPort.Text);
            }
            catch
            {
                CloseCancel = true;
                MessageBox.Show("端口必须为数字");
                return;
            }

            try
            {
                Convert.ToInt32(txgMaxNum.Text);
            }
            catch
            {
                CloseCancel = true;
                MessageBox.Show("最大采集数必须为数字");
                return;
            }

            entity.Enabled = chkEnable.Checked;
            entity.MaxNum = Convert.ToInt32(txgMaxNum.Text);
            entity.PortName = Convert.ToInt32(txtPort.Text);
            entity.SendFile = txtFile.Text;

            using (CaijiDataContext db = CaijiDataContext.GetDataContext()) 
            {
                if (entity.ID == 0)
                {
                    if (db.Ports.Any(T => T.PortName == entity.PortName))
                    {
                        CloseCancel = true;
                        MessageBox.Show("端口号已经存在");
                        return;
                    }

                    db.Ports.InsertOnSubmit(entity);
                    db.SubmitChanges();
                }
                else 
                {
                    if (db.Ports.Any(T => T.PortName == entity.PortName && T.ID != entity.ID))
                    {
                        CloseCancel = true;
                        MessageBox.Show("端口号已经存在");
                        return;
                    }

                    db.Ports.Attach(entity);
                    db.Refresh(RefreshMode.KeepCurrentValues, entity);
                    db.SubmitChanges(ConflictMode.ContinueOnConflict);
                }
            }

            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = false;
            ofd.Filter = "文本文件*.ini|*.ini";
            if (ofd.ShowDialog() == DialogResult.OK) 
            {
                txtFile.Text = ofd.FileName;
            }
        }


    }
}
